package com.starbaba.mall.view;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.base.common.log.LogService;
import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.common.ResultJson;
import com.starbaba.controllog.aop.LogAble;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.mall.service.MallItemTopicService;
import com.starbaba.mall.service.MallTopicService;
import com.starbaba.mall.service.ProductItemService;
import com.starbaba.mall.service.impl.MallItemTopicServiceImpl;
import com.starbaba.mall.service.impl.MallTopicServiceImpl;
import com.starbaba.mall.service.impl.ProductItemServiceImpl;
import com.tuanzi.server.loan_base.model.db.Loan_Mall_Item;
import com.tuanzi.server.loan_base.model.db.Loan_Mall_Item_Topic;
import com.tuanzi.server.loan_base.model.db.Loan_Mall_Topic;

/**
 * 天猫返利专题、商品关联Action
 * @author chenweijun
 *
 */
@SuppressWarnings({"serial","unchecked","rawtypes","unused"})
public class MallItemTopicAction extends BaseAction implements ModelDriven<Loan_Mall_Item_Topic>{

	private Loan_Mall_Item_Topic model;
	private MallTopicService topicService;
	private ProductItemService itemService;
	private MallItemTopicService itemTopicService;
	
	public MallItemTopicAction(){
		this.topicService = new MallTopicServiceImpl();
		this.itemService = new ProductItemServiceImpl();
		this.itemTopicService = new MallItemTopicServiceImpl();
	}
	
	
	@Override
	public Loan_Mall_Item_Topic getModel() {
		if(model  == null){
			model = new Loan_Mall_Item_Topic();
		}
		return model;
	}
	
	
	/**
	 * 查询专题列表
	 */
	public void getTopicList(){
		
		parsePageRequest();
		PageResponse pageResponse=new PageResponse();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		List<Loan_Mall_Topic> list = topicService.getTopicList(paramsMap, pageRequest.getIDisplayStart(),pageRequest.getIDisplayLength());
		int count=topicService.countTopic(paramsMap);
		pageResponse.setITotalDisplayRecords(count);
		pageResponse.setITotalRecords(count);
		try {
			pageResponse.setAaData(list);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch(IOException e) {
			LogService.error(e);
		}
	}
	
	/**
	 * 添加主题商品
	 */
	public void addTopicItem(){
		parsePageRequest();
		PageResponse pageResponse=new PageResponse();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
	}
	
	/**
	 * 删除专题下的商品
	 * @return
	 * @throws IOException 
	 */
	public void deleteTopicItem() throws IOException{
		
		parsePageRequest();
		PageResponse pageResponse=new PageResponse();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		String topic_id = (String) paramsMap.get("topic_id");
		String item_source_id = (String) paramsMap.get("item_source_id");
		int result = itemTopicService.deleteLoan_Mall_Item(Long.parseLong(topic_id), item_source_id);
		if(result > 0){
			JsonObject response = new JsonObject();
			response.addProperty("result", "删除成功");
			sendJsonMessage(response.toString());
		}
		
	}
	
	/**
	 * 排序
	 */
	@LogAble(operate = LogAble.OPERATE_MODIFY)
	public void sortTab() {
		parsePageRequest();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		itemTopicService.sortTab(paramsMap);
		responseSuccess();
	
	}
	
	private void responseSuccess() {
		JsonObject result = new JsonObject();
		result.add("result", new ResultJson().toJSON());
		try {
			sendJsonMessage(result.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void responseError(Exception error) {
		StringWriter writer = new StringWriter();
		error.printStackTrace(new PrintWriter(writer, true));
		ResultJson resultJson = new ResultJson(-2, 0, writer.toString());
		JsonObject result = new JsonObject();
		result.add("result", resultJson.toJSON());
		try {
			sendJsonMessage(result.toString());
		} catch (Exception e) {
			LogService.error(e);
		}
	}
	

}
